import 'dart:convert';

import 'package:baixing/pages/shouye/ad_banner.dart';
import 'package:baixing/pages/shouye/floor_content.dart';
import 'package:baixing/pages/shouye/floor_title.dart';
import 'package:baixing/pages/shouye/leader_phone.dart';
import 'package:baixing/pages/shouye/recommend.dart';
import 'package:baixing/pages/shouye/swiper_diy.dart';
import 'package:baixing/pages/shouye/top_navigator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import '../service/service_method.dart';
import 'shouye/hot_goods.dart';

//首页
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

//保存页面状态分为三步：
//1、 with AutomaticKeepAliveClientMixin
//2、   @override
//        bool get wantKeepAlive => true;
//3、  IndexedStack(
//        index: currentIndex,
//        children: tabBodies,
//      ),

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  int page = 1;
  List<Map> hotGoodsList = [];

  GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<
      RefreshFooterState>();

  @override
  bool get wantKeepAlive => true;

  String homePageContent = "正在获取数据";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("百姓生活+"),
          elevation: 0,
        ),
        body: FutureBuilder(
          future: request("homePageContent",
              formData: {"lon": "115.02932", "lat": "35.76189"}),
          //异步操作
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              //是否有值
              var data = json.decode(snapshot.data.toString());
              print("输出的数据=$data");
              List<Map> swiper =
              (data["data"]["slides"] as List).cast(); //头部Banner
              List<Map> navgatorList =
              (data["data"]["category"] as List).cast(); //10个Item

              String adpicUrl =
              data["data"]["advertesPicture"]["PICTURE_ADDRESS"]; //AdBanner
              String leaderImg = data["data"]["shopInfo"]["leaderImage"]; //店长图片
              String leaderMoble =
              data["data"]["shopInfo"]["leaderPhone"]; //店长电话

              List<Map> recommendList =
              (data["data"]["recommend"] as List).cast(); //商品推荐

              String floor1Title =
              data["data"]["floor1Pic"]["PICTURE_ADDRESS"]; //楼层1
              List<Map> floor1 = (data["data"]["floor1"] as List).cast(); //五条数据

              String floor2Title =
              data["data"]["floor2Pic"]["PICTURE_ADDRESS"]; //楼层1
              List<Map> floor2 = (data["data"]["floor2"] as List).cast(); //五条数据

              String floor3Title =
              data["data"]["floor3Pic"]["PICTURE_ADDRESS"]; //楼层1
              List<Map> floor3 = (data["data"]["floor3"] as List).cast(); //五条数据

              return EasyRefresh(
                refreshFooter: ClassicsFooter(
                  key: _footerKey,
                  bgColor: Colors.white,
                  textColor: Colors.pink,
                  moreInfoColor: Colors.pink,
                  showMore: true,
                  noMoreText: "",
                  moreInfo: "加载中……",
                  loadReadyText: "上拉加载……",
                ),

                child: ListView(
                  children: <Widget>[
                    SwiperDiy(swiperDateList: swiper),
                    TopNavigator(navgatorList: navgatorList),
                    AdBanner(adPic: adpicUrl),
                    LeaderPhone(
                        leaderImage: leaderImg, leaderPhone: leaderMoble),
                    Recommend(recommendList: recommendList),
                    FloorTitle(picture_address: floor1Title),
                    FloorContent(floorGoodsList: floor1),
                    FloorTitle(picture_address: floor2Title),
                    FloorContent(floorGoodsList: floor2),
                    FloorTitle(picture_address: floor3Title),
                    FloorContent(floorGoodsList: floor3),
                    HotGoods(page: page, hotGoodsList: hotGoodsList),
                  ],
                ),
                loadMore: () async {
                  print("开始加载更多……");
                  var formData = {"page": page};
                  await request("homePageBelowConten", formData: formData)
                      .then((onValue) {
                    var data = json.decode(onValue.toString());
                    List<Map> newGoodsList = (data["data"] as List).cast();
                    setState(() {
                      hotGoodsList.addAll(newGoodsList);
                      page++;
                    });
                  });
                },
                onRefresh: () {
                  print("开始下拉刷新……");
                },
              );
            } else {
              return Center(child: Text("加载中……"));
            }
          },
        ));
  }
}
